package com.ys.controller.web.business;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ys.service.business.ThrCanvassApplyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.entity.business.ThrCanvassApply;
import com.ys.entity.business.ThrCanvassApplyItem;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

import lombok.AllArgsConstructor;
import lombok.Data;


@Controller
@RequestMapping("/web/business/thrCanvassApply")
public class WebThrCanvassApplyController {
    @Autowired
    private CommonApiService<ThrCanvassApply> commonApiService;

    @Autowired
    private ThrCanvassApplyService thrCanvassApplyService;
    
    /*
     * 新增
     */
    @RequiresPermissions("thrCanvassApply:add")
    @GetMapping("/toAdd")
    public String toAdd(Model model) throws Exception{
        String bid = commonApiService.getCommonNextBid(MenuId.THR_CanvassApply);
        model.addAttribute("onProcess", 0);
        String aa = commonApiService.toAdd(model, new ThrCanvassApply().setBid(bid), MenuId.THR_CanvassApply, null, null);
        
        if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
            return "business/thrCanvassApply/thrCanvassApplyAdd";
		} else {
			return aa;
		}
    }
    /*
     * 编辑
     */
    @RequiresPermissions("thrCanvassApply:add")
    @GetMapping("/toEdit")
    public String toEdit(Model model, String bid) throws Exception{
        Map<String,Object> entity = thrCanvassApplyService.getExtraParam(bid);
        if (entity == null){
            return W.error(model, "已删除!");
        }

        List<Map<String, Object>> itemList = thrCanvassApplyService.getItemList(bid);


        YsProcInst yspProcInst = new YsProcInst().setBid(bid).queryFirst();
        Integer onProcess = yspProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;

        model.addAttribute("onProcess", onProcess);
        DetailList detailList = new DetailList(itemList, (Long) entity.get("id"));
        model.addAttribute("detail", detailList);

        commonApiService.toEdit(model, (Long) entity.get("id"), new ThrCanvassApply(), MenuId.THR_CanvassApply, entity, null);

        return "business/thrCanvassApply/thrCanvassApplyEdit";
    }

    @Data
    @AllArgsConstructor
    private static class DetailList{
        private List<Map<String, Object>> itemList;
        private Long id;
    }

}
